﻿using IcsSEMCtrl.Db;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TKT.Lifter.CIM.Lifter.Gui.Monitor.Panels
{
    public partial class ChartStatisticsControl : UserControl
    {
        List<int> transferQTYMonth = new List<int>();
        List<double> utiRateMonth = new List<double>();
        List<int> transferQTYWeek = new List<int>();
        List<double> utiRateWeek = new List<double>();
        List<int> transferQTYDay = new List<int>();
        List<double> utiRateDay = new List<double>();
        public Dictionary<string, string> chartControlDict = new Dictionary<string, string>()
        {
            {"",""},
            {"每月BOX数量","MonthBoxQTY"},
            {"每周BOX数量","WeekBoxQTY"} ,
            {"每天BOX数量","DayBoxQTY"} ,
            {"每月稼动率","UtiRateMonth"} ,
            {"每周稼动率","UtiRateWeek"} ,
            {"每天稼动率","UtiRateDay"}
        };
        public ChartStatisticsControl()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 更新数据统计图的数据信息。
        /// </summary>
        public void updateChartData()
        {
            //获取月数的横坐标
            string[] month = new string[] { DateTime.Now.AddMonths(-3).ToString("MMM"), DateTime.Now.AddMonths(-2).ToString("MMM"), DateTime.Now.AddMonths(-1).ToString("MMM") };

            //获取周数的横坐标
            GregorianCalendar gregorianCalendar = new GregorianCalendar();
            //获取当前是第几周（以周一开始计算）
            int weeks = gregorianCalendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
            string[] weekArray = new string[] { "WW" + (weeks - 4), "WW" + (weeks - 3), "WW" + (weeks - 2), "WW" + (weeks - 1) };

            //获取天数的横坐标
            string[] dayArray = new string[]
            {
                DateTime.Now.AddDays(-7).ToString("MM/dd"),
                DateTime.Now.AddDays(-6).ToString("MM/dd"),
                DateTime.Now.AddDays(-5).ToString("MM/dd"),
                DateTime.Now.AddDays(-4).ToString("MM/dd"),
                DateTime.Now.AddDays(-3).ToString("MM/dd"),
                DateTime.Now.AddDays(-2).ToString("MM/dd"),
                DateTime.Now.AddDays(-1).ToString("MM/dd")
            };

            //清空之前所有的数据
            this.transferQTYMonth.Clear();
            this.utiRateMonth.Clear();
            this.transferQTYWeek.Clear();
            this.utiRateWeek.Clear();
            this.transferQTYDay.Clear();
            this.utiRateDay.Clear();

            //获取所需数据
            this.GetDayData();
            this.GetWeekData();
            this.GetMonthData();
            this.chart3.Series[0].Points.DataBindXY(month, transferQTYMonth.ToArray());//每月搬送数量
            this.chart3.Series[3].Points.DataBindXY(month, utiRateMonth.ToArray());//每月稼动率
            this.chart3.Series[1].Points.DataBindXY(weekArray, transferQTYWeek.ToArray());//每周搬送数量
            this.chart3.Series[4].Points.DataBindXY(weekArray, utiRateWeek.ToArray());//每周稼动率
            this.chart3.Series[2].Points.DataBindXY(dayArray, transferQTYDay.ToArray());//每日搬送数量
            this.chart3.Series[5].Points.DataBindXY(dayArray, utiRateDay.ToArray());//每日稼动率
        }
        private void GetWeekData()
        {
            DBConnection dBConnection = DBControl.GetConnection();
            SqlDataReader sqlDataReader = null;
            //获取搬送数量
            for (int i = 4; i > 0; i--)
            {
                DateTime mdate = DateTime.Today;
                DateTime sdate = DateTime.Today.AddDays(1).AddSeconds(-1);
                int m = (mdate.DayOfWeek == DayOfWeek.Sunday ? (DayOfWeek)7 : mdate.DayOfWeek) - DayOfWeek.Monday;
                int s = (sdate.DayOfWeek == DayOfWeek.Sunday ? (DayOfWeek)7 : sdate.DayOfWeek) - (DayOfWeek)7;
                int week = -7 * i;
                DateTime Mon = mdate.AddDays((week - m));
                DateTime Sun = sdate.AddDays((week - s));

                string sqlString = string.Format("SELECT SUM(TRANSPORTQTY) AS QTY,round(AVG(UTILIZATIONRATE),2) AS UTI,round(AVG(IDLERATE),2) AS IDLE  FROM DATASTATISTICS WHERE  '{0}' <= DATETIME and DATETIME <= '{1}'", Mon, Sun);
                sqlDataReader = dBConnection.ExecuteQuery(sqlString);
                while (sqlDataReader.Read())
                {
                    if (!string.IsNullOrEmpty(sqlDataReader["QTY"].ToString()))
                    {
                        transferQTYWeek.Add((int)sqlDataReader["QTY"]);
                    }
                    else
                    {
                        transferQTYWeek.Add(0);
                    }
                    if (!string.IsNullOrEmpty(sqlDataReader["UTI"].ToString()))
                    {
                        utiRateWeek.Add((double)sqlDataReader["UTI"]);
                    }
                    else
                    {
                        utiRateWeek.Add(0);
                    }
                }
            }

            if (sqlDataReader != null)
            {
                sqlDataReader.Close();
            }
            DBControl.RemoveDBConection(dBConnection);
        }
        /// <summary>
        /// 获取前三个月的搬送数量，稼动率，闲置率
        /// </summary>
        private void GetMonthData()
        {
            DBConnection dBConnection = DBControl.GetConnection();
            //获取搬送数量
            List<string> monthList = new List<string>()
            {
                DateTime.Now.AddMonths(-3).ToString("yyyy-MM"),
                DateTime.Now.AddMonths(-2).ToString("yyyy-MM"),
                DateTime.Now.AddMonths(-1).ToString("yyyy-MM")
            };


            SqlDataReader sqlDataReader = null;
            foreach (string item in monthList)
            {
                string sqlString = string.Format("SELECT SUM(TRANSPORTQTY) AS QTY,round(AVG(UTILIZATIONRATE),2) AS UTI,round(AVG(IDLERATE),2) AS IDLE  FROM DATASTATISTICS WHERE CREATETIME like '{0}%'", item);
                sqlDataReader = dBConnection.ExecuteQuery(sqlString);
                while (sqlDataReader.Read())
                {
                    if (!string.IsNullOrEmpty(sqlDataReader["QTY"].ToString()))
                    {
                        transferQTYMonth.Add((int)sqlDataReader["QTY"]);
                    }
                    else
                    {
                        transferQTYMonth.Add(0);
                    }
                    if (!string.IsNullOrEmpty(sqlDataReader["UTI"].ToString()))
                    {
                        utiRateMonth.Add((double)sqlDataReader["UTI"]);
                    }
                    else
                    {
                        utiRateMonth.Add(0);
                    }
                }
            }

            if (sqlDataReader != null)
            {
                sqlDataReader.Close();
            }
            DBControl.RemoveDBConection(dBConnection);
        }
        private void GetDayData()
        {
            DBConnection dBConnection = DBControl.GetConnection();
            //获取搬送数量
            List<string> dayList = new List<string>()
            {
                DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd"),
                DateTime.Now.AddDays(-6).ToString("yyyy-MM-dd"),
                DateTime.Now.AddDays(-5).ToString("yyyy-MM-dd"),
                DateTime.Now.AddDays(-4).ToString("yyyy-MM-dd"),
                DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd"),
                DateTime.Now.AddDays(-2).ToString("yyyy-MM-dd"),
                DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd")
            };


            SqlDataReader sqlDataReader = null;
            foreach (string item in dayList)
            {
                string sqlString = string.Format("SELECT TRANSPORTQTY AS QTY,UTILIZATIONRATE AS UTI,IDLERATE AS IDLE  FROM DATASTATISTICS WHERE CREATETIME = '{0}'", item);
                sqlDataReader = dBConnection.ExecuteQuery(sqlString);
                if (sqlDataReader.Read())
                {
                    transferQTYDay.Add((int)sqlDataReader["QTY"]);
                    utiRateDay.Add((double)sqlDataReader["UTI"]);
                }
                else
                {
                    transferQTYDay.Add(0);
                    utiRateDay.Add(0);
                }
            }

            if (sqlDataReader != null)
            {
                sqlDataReader.Close();
            }
            DBControl.RemoveDBConection(dBConnection);
        }
        /// <summary>
        /// 更新图表控件颜色
        /// </summary>
        public void updateChartColor(string controlName, Color color)
        {
            if (color.IsEmpty || color.Name.Equals("0"))
            {
                return;
            }
            if (this.chart3.Series.FindByName(controlName) != null)
            {
                this.chart3.Series.FindByName(controlName).Color = color;
            }
        }
    }
}
